<%  //--------------------------------------------------------------------------------
    // File             : menu.jsp
    // Description      : This is a sample JSP code to generate a VXML menu.
    // Input parameters : - maintainer -- email address to receive trace log. (optional)
    //                    - choiceX    -- choices of the menu.
    //                    - urlX       -- URLs of choices.
    //                    - audioX     -- wave files to play audio choices. (optional)
    //                      where X is 1,2,3... etc.
    // Output           : A VXML document with a menu of choices that have the
    //                    corresponding url and audio file for each choice.
    //
    // Example:
    //   URL   : http://cafe.bevocal.com/libraries/jsp/menu.jsp?maintainer=test@bevocal.com&choice1=Test one&choice2=Test two&audio1=audio1.wav&audio2=audio2.wav&url1=test1.vxml&url2=test2.vxml
    //   Result: 
    //           <?xml version="1.0" ?>
    //           <vxml version="1.0">
    //             <meta name="maintainer" content="test@bevocal.com" />
    //             <menu accept="approximate">
    //               <prompt> Welcome to menu generator. Please choose one of the following <enumerate/> </prompt>
    //               <choice next="test1.vxml"><audio src="audio1.wav">Test one</audio></choice>
    //               <choice next="test2.vxml"><audio src="audio2.wav">Test two</audio></choice>
    //             </menu>
    //             <help>I'm sorry. There's no help available here.</help>
    //             <noinput> I'm sorry. I didn't hear anything. <reprompt /> </noinput>
    //             <nomatch> I didn't get that. <reprompt /> </nomatch>
    //           </vxml>
    //
    // Notes:
    //   - maintainer and audio inputs are optional.
    //   - For each choice name, there must be a corresponding url name, i.e. choice5 
    //     requires url5. Otherwise, the choice will not be generated.
    //   - If the input URL has passing parameters, the URL must be encoded before 
    //     passing to menu.jsp. 
    //--------------------------------------------------------------------------------
    response.setContentType("application/voicexml+xml");

    out.println("<?xml version=\"1.0\" ?>");
    
    //--------------------------------------------------------------------------------
    //Include files go here:
    //--------------------------------------------------------------------------------%>
<%@ page import="java.util.*" %>
<%@ page import="java.net.URLEncoder" %>

<%	//--------------------------------------------------------------------------------
	//Gathering input parameters including maintainer and a list of choice names:
	//--------------------------------------------------------------------------------
	//Obtaining maintainer value:
	String maintainer = request.getParameter("maintainer");
	if (maintainer == null) { maintainer = ""; }
	//Obtaining all parameter names of the request:
	Enumeration paramNames = request.getParameterNames();
	//Constructing a vector of choice names from the list of parameter names:
	Vector choices = new Vector();
	if (paramNames != null) {
		while (paramNames.hasMoreElements()) {
			String tempStr = (String) paramNames.nextElement();
			if (tempStr.indexOf("choice") == 0) {
				choices.add(tempStr);
			}
		}
	}
	//--------------------------------------------------------------------------------
	//Generating vxml menu:
	//--------------------------------------------------------------------------------
	//Generating VXML header:
	out.println("<vxml version=\"1.0\">");
	//Generating meta tag for maintainer if needed:
	if (!maintainer.equals("")) {
		out.println("<meta name=\"maintainer\" content=\"" + maintainer + "\" />");
	}
	//Generating the menu if there is at least one choice:
	int numChoices = choices.size();
	if (numChoices > 0) {
		try {
			//Generating menu tag and welcome prompt:
			out.println("<menu accept=\"approximate\">");
			out.println("  <prompt> Welcome to menu generator. Please choose one of the following <enumerate/> </prompt>");
			//Generating choices for valid input choice and url:
			for (int i = numChoices - 1; i >= 0; i--) {
				//Obtaining choice number, i.e. 1 or 2 or 3 etc.
				String suffix = ((String) choices.elementAt(i)).substring(6);
				//Obtaining the value for a choice including choice value, url and audio file.
				String tempChoice = request.getParameter("choice" + suffix);
				String tempUrl    = request.getParameter("url" + suffix);
				String tempAudio  = request.getParameter("audio" + suffix);
				//Do not generate choice for unavailable url:
				if (tempUrl != null && !tempUrl.equals("")) {
					//Generating choice with audio tag if audio file is available:
					if (tempAudio != null && !tempAudio.equals("")) {
						out.println("  <choice next=\"" + URLEncoder.encode(tempUrl) + "\"><audio src=\"" + tempAudio + "\">" + tempChoice + "</audio></choice>");
					}
					//Generating choice with no audio tag:
					else {
						out.println("  <choice next=\"" + URLEncoder.encode(tempUrl) + "\">" + tempChoice + "</choice>");
					}
				}
			}
			out.println("</menu>");
			//Generating default prompt for help, no input or no match:
			out.println("<help>I'm sorry. There's no help available here.</help>");
			out.println("<noinput> I'm sorry. I didn't hear anything. <reprompt /> </noinput>");
			out.println("<nomatch> I didn't get that. <reprompt /> </nomatch>");
		}
		catch (Exception e) {
			out.println("Exception: " + e.toString());
		}
	}
	//Generate no choice message if there is no input choice.
	else {
		out.println("<form id=\"mainmenu\">");
		out.println("  <block><prompt> Welcome to menu generator. There is no choice. Good bye. </prompt></block>");
		out.println("</form>");
	}
	//Generating the close vxml tag:
	out.println("</vxml>");
%>